package com.frinder.robot.strategy.task.convert.biz;

import com.frinder.robot.handler.task.RobotTask;
import com.frinder.robot.strategy.task.convert.ConvertToRobotTaskBaseStrategy;
import com.frinder.robot.strategy.task.convert.ConvertToRobotTaskPayload;
import com.frinder.robot.strategy.task.convert.payload.ConvertToRobotTransportTaskPayload;
import com.frinder.robot.task.meta.ForkPalletRobotTask;
import com.frinder.robot.task.meta.PointToPointRobotTask;
import com.frinder.robot.task.meta.PutDownPalletRobotTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

import static com.frinder.robot.strategy.task.convert.ConvertToRobotTaskPayload.Type.TRANSPORT;

/**
 * @ClassName ConvertToRobotTransportTaskStrategy
 * @Description 转化任务为机器人搬运任务
 * @Author frinder
 * @Date 2019/10/18 13:52
 * @Verion 1.0
 */
@Slf4j
@Component
public class ConvertToRobotTransportTaskStrategy extends ConvertToRobotTaskBaseStrategy {
    @Override
    public void handle(ConvertToRobotTaskPayload payload) {
        if (Objects.equals(payload.getType(), TRANSPORT)) {
            ConvertToRobotTransportTaskPayload transportTaskPayload = (ConvertToRobotTransportTaskPayload) payload;
            String taskId = transportTaskPayload.getTaskId();
            String originNode = transportTaskPayload.getOriginNode();
            String destNode = transportTaskPayload.getDestNode();
            RobotTask task = RobotTask.builder()
                    .taskId(taskId)
                    .build()
                    // 机器人从当前位置到起点任务
                    .add(PointToPointRobotTask.builder().originNode(null).destNode(originNode).build())
                    // 机器人叉起托盘任务
                    .add(ForkPalletRobotTask.builder().forkPalletNode(originNode).build())
                    // 机器人搬运货物从起点到目标点任务
                    .add(PointToPointRobotTask.builder().originNode(originNode).destNode(destNode).build())
                    // 机器人放下托盘任务
                    .add(PutDownPalletRobotTask.builder().putDownPalletNode(destNode).build());
            // 添加任务
            add(task);
            log.info("{}-{}", TRANSPORT, task);
        } else {
            super.handle(payload);
        }
    }

}
